PRG 1.0 TXT

/*
*/

//-----------------------------------------------------------------------------
// [CONFIG]

include "..\program_include.ocf"

object Program
{
	type		= e_program.NORMAL_AT;
	layer_bits	= e_render_layer_bits.OPAQUE;
	
	ubo_data	= array /*UBO*/ {
		{ "inMaterial",  e_program_data.MATERIAL }
	}

	textures	= array /*ProgramTexture*/ {
		{ "unDiffuseMap", e_texture.DIFFUSE },
		{ "unNormalMap",  e_texture.NORMAL }
	}
	
	uniforms	= array /*string*/ {
		"transform", "unTransform."
	}
}


//-----------------------------------------------------------------------------
// [VERTEX]

#version 330 core
#pragma optimize(on)

#define ATTR_POSITION	0
#define ATTR_NORMAL		1
#define ATTR_TEXCOORD	2

layout(location = ATTR_POSITION)	in vec3  inPosition;
layout(location = ATTR_NORMAL)		in vec3  inNormal;
layout(location = ATTR_TEXCOORD)	in vec2  inTexcoord;

uniform struct TTransform {
	mat4	MVP;
	mat4	MV;
	mat3	Normal;
} unTransform;


out	TVertexData {
	vec3	vPosition;
	vec2	vTexcoord;
	vec3	vNormal;
	float	fDepth;
} Output;


void main()
{
	gl_Position		 = unTransform.MVP * vec4(inPosition, 1.0);
	Output.vPosition = inPosition;
	Output.fDepth	 = ( unTransform.MV * vec4(inPosition, 1.0) ).z;
	Output.vTexcoord = inTexcoord;
	Output.vNormal	 = inNormal * unTransform.Normal;
}


//-----------------------------------------------------------------------------
// [GEOMETRY]

#version 330 core
#pragma optimize(on)

layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;

in	TVertexData {
	vec3	vPosition;
	vec2	vTexcoord;
	vec3	vNormal;
	float	fDepth;
} Input[];

out	TGeomData {
	vec3	vNormal;
	vec3	vTangent;
	vec3	vBinormal;
	vec2	vTexcoord;
	float	fDepth;
} Output;


void CalcTBN(in vec3 vPosition0, in vec2 vTexcoord0,
			 in vec3 vPosition1, in vec2 vTexcoord1,
			 in vec3 vPosition2, in vec2 vTexcoord2,
			 out vec3 vNormal, out vec3 vTangent, out vec3 vBinormal)
{
	float	fBumpFactor = 0.1;
	vec3	sw;
	vec3	u  = vec3( vTexcoord0.x, vTexcoord1.x, vTexcoord2.x );
	vec3	v  = vec3( vTexcoord0.y, vTexcoord1.y, vTexcoord2.y );
	vec3	id = vec3( 1.0, 1.0, 1.0 );
	
	sw = cross( u, id );
	sw = sw * ( fBumpFactor / dot(v, sw) );
	
	vTangent = vPosition0 * sw.x + vPosition1 * sw.y + vPosition2 * sw.z;
	
	sw = cross( v, id );
	sw = sw * ( fBumpFactor / dot(u, sw) );
	
	vBinormal = vPosition0 * sw.x + vPosition1 * sw.y + vPosition2 * sw.z;
	
	vNormal = normalize( cross( vTangent, vBinormal ) );
}


void main()
{
	vec3	normal;
	vec3	tangent;
	vec3	binormal;
	
	CalcTBN( Input[0].vPosition, Input[0].vTexcoord,
			 Input[1].vPosition, Input[1].vTexcoord,
			 Input[2].vPosition, Input[2].vTexcoord,
			 normal, tangent, binormal );

	gl_Position		 = gl_in[0].gl_Position;
	Output.vNormal	 = Input[0].vNormal;
	Output.vTexcoord = Input[0].vTexcoord;
	Output.fDepth	 = Input[0].fDepth;
	Output.vBinormal = binormal;
	Output.vTangent  = tangent;
	EmitVertex();
	
	gl_Position		 = gl_in[1].gl_Position;
	Output.vNormal	 = Input[1].vNormal;
	Output.vTexcoord = Input[1].vTexcoord;
	Output.fDepth	 = Input[1].fDepth;
	Output.vBinormal = binormal;
	Output.vTangent  = tangent;
	EmitVertex();
	
	gl_Position		 = gl_in[2].gl_Position;
	Output.vNormal	 = Input[2].vNormal;
	Output.vTexcoord = Input[2].vTexcoord;
	Output.fDepth	 = Input[2].fDepth;
	Output.vBinormal = binormal;
	Output.vTangent  = tangent;
	EmitVertex();
	
	EndPrimitive();
}


//-----------------------------------------------------------------------------
// [FRAGMENT]

#version 330 core
#pragma optimize(on)

#define ALPHA_REF	0.1

layout(location = 0)	out vec4	outDNNS;	// depth, normal(x,y), Shininness
layout(location = 1)	out vec4	outRGBM;	// colorRGB, MaterialID

uniform sampler2D	unDiffuseMap;
uniform sampler2D	unNormalMap;

layout(std140)
uniform inMaterial {
	vec3	vAmbientColor;
	vec4	vDiffuseColor;
	vec3	vSpecularColor;
	vec3	vEmissionColor;
	float	fShininess;
} unMaterial;


in	TGeomData {
	vec3	vNormal;
	vec3	vTangent;
	vec3	vBinormal;
	vec2	vTexcoord;
	float	fDepth;
} Input;


vec2 CalcOutputNormal(vec3 vNormal)
{
	vec2	res = ( vNormal.xy + vec2(1.0) ) * 0.5;
	res.x *= ( vNormal.z < 0.0 ? -1.0 : 1.0 );
	return res;
}


void main()
{
	vec4	color    = texture( unDiffuseMap, Input.vTexcoord ) * unMaterial.vDiffuseColor;
	
	if ( color.a < ALPHA_REF )
		discard;

	vec3	norm_map = texture( unNormalMap, Input.vTexcoord ).rgb * 2.0 - vec3(1.0);
	vec3	normal	 = normalize( norm_map.x * Input.vTangent  +
								  norm_map.y * Input.vBinormal +
								  norm_map.z * Input.vNormal );

	outDNNS.r   = Input.fDepth;
	outDNNS.gb  = CalcOutputNormal( normal );
	outDNNS.a	= unMaterial.fShininess;
	outRGBM.rgb	= color.rgb;
	outRGBM.a   = 0.0;
}

// [END]
